package com.xc.disk.web.rest;

import com.xc.disk.bean.DrawCodeBean;
import com.xc.disk.bean.ShareBean;
import com.xc.disk.dto.DrawCodeDto;
import com.xc.disk.dto.FolderDto;
import com.xc.disk.entity.ShareEntity;
import com.xc.disk.exception.OperateException;
import com.xc.disk.info.TokenInfo;
import com.xc.disk.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>项目名称: Stars</p>
 * <p>描述: 共享文件</p>
 * <P>创建人:小程 </p>
 * <P>创建人邮箱: 779943476@qq.com</p>
 * <p>创建时间: 2019/11/25</p>
 *
 * @version v1.0
 */
@RestController
public class ShareRest {

    @Autowired
    private ShareService shareService;

    /**
     * 创建共享文件
     *
     * @param token     用户token信息
     * @param shareBean 文件复制参数
     * @return 创建成功的信息
     */
    @PostMapping("/share/file")
    public ShareEntity createShare(TokenInfo token, @RequestBody ShareBean shareBean) {
        return shareService.createShare(token, shareBean);
    }

    /**
     * 获取当前用户的共享文件
     *
     * @param token 用户token信息
     * @return 前用户的共享文件集合
     */
    @GetMapping("/share/file/list")
    public List<ShareEntity> getShareFileList(TokenInfo token) {
        return shareService.getShareFileListByUserId(token);
    }

    /**
     * 取消共享
     *
     * @param token 用户token信息
     * @param ids   临时文件id集合
     */
    @DeleteMapping("/share/file/{ids}")
    public void deleteShareFile(TokenInfo token, @PathVariable List<String> ids) {
        shareService.deleteShareFile(token, ids);
    }


    /**
     * 验证共享文件是否需要提取码
     *
     * @param code 共享文件代码
     * @return 不需要提取码返回共享id
     */
    @GetMapping("/share/inspect/{code}")
    public DrawCodeDto inspectDrawCode(@PathVariable String code) {
        return shareService.inspectDrawCode(code);
    }

    /**
     * 验证提取码
     *
     * @param drawCodeBean 共享文件参数
     * @return 提取码正确，返回共享id
     */
    @PostMapping("/share/verify")
    public DrawCodeDto verifyDrawCode(@RequestBody DrawCodeBean drawCodeBean) {
        return shareService.verifyDrawCode(drawCodeBean);
    }

    /**
     * 查询共享文件
     *
     * @param fid      共享出去的code
     * @param parentId 父级目录id
     * @return 共享文件信息
     */
    @GetMapping("/share/file/{fid}")
    public FolderDto getShareFile(@PathVariable String fid, @RequestParam(required = false) String parentId) {
        return shareService.getShareFile(fid, parentId);
    }

    /**
     * 下载共享文件
     *
     * @param shareId 共享文件id
     * @param fid     文件id
     */
    @GetMapping("/share/download/{shareId}")
    public void downloadShareFile(@PathVariable String shareId, @RequestParam String fid, HttpServletResponse response) {
        try {
            if (fid.contains(".")) {
                fid = fid.substring(0, fid.lastIndexOf("."));
            }
            String url = shareService.downloadShareFile(shareId, fid);
            response.sendRedirect(url);
        } catch (IOException e) {
            throw new OperateException("f015", "重定向下载失败！");
        }
    }


    /**
     * 保存共享文件
     *
     * @param token    用户token
     * @param shareId  共享文件id
     * @param folderId 目录id
     */
    @GetMapping("/share/save/{shareId}")
    public void saveShare(TokenInfo token, @PathVariable String shareId, @RequestParam String folderId) {
        shareService.saveShare(token, shareId, folderId);
    }

}
